package com.good.child.ui.activity.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.atech.glcamera.utils.FilterFactory;
import com.atech.glcamera.views.GLCameraView;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.good.child.R;
import com.good.child.iview.EmptyIView;
import com.good.child.pressent.EmptyPresenter;
import com.google.android.material.tabs.TabLayout;
import com.yimidida.library.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.constraintlayout.widget.ConstraintLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create on: 2024/2/27
 * by Mr.Zhu
 * Description: TODO
 */
public class CameraActivityNew extends BaseActivity<EmptyIView, EmptyPresenter> {

    private final static String TAG = "CameraActivity";
    private static Context me;
    GLCameraView cameraView;
    @BindView(R.id.iv_switch)
    AppCompatImageView ivSwitch;
    @BindView(R.id.iv_shot)
    AppCompatImageView ivShot;
    @BindView(R.id.iv_type_switch)
    AppCompatImageView ivTypeSwitch;
    @BindView(R.id.cl_Pic)
    ConstraintLayout clPic;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.iv_close)
    ImageView ivClose;

    @BindView(R.id.iv_texiao_close)
    ImageView ivTexiaoClose;

    @BindView(R.id.cl_menu)
    ConstraintLayout clMenu;
    @BindView(R.id.tv_tx_1)
    TextView tv_tx_1;
    @BindView(R.id.tab_layout1)
    TabLayout tabLayout1;
    private List<FilterFactory.FilterType> filters = new ArrayList<>();

    @Override
    protected EmptyPresenter initPresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_new);
        ButterKnife.bind(this);
        cameraView = findViewById(R.id.glcamera);
        me = this;
        initView();
    }

    @Override
    protected void initView() {
        filters.add(FilterFactory.FilterType.Original);
        filters.add(FilterFactory.FilterType.Sunrise);
        filters.add(FilterFactory.FilterType.Sunset);
        filters.add(FilterFactory.FilterType.BlackWhite);
        filters.add(FilterFactory.FilterType.WhiteCat);
        filters.add(FilterFactory.FilterType.BlackCat);
        filters.add(FilterFactory.FilterType.SkinWhiten);
        setStatusBar(false, false);
        setAndroidNativeLightStatusBar(this, true);
        setTitle("相机");
        TabLayout.Tab tab1 = tabLayout1.newTab();
        TabLayout.Tab tab2 = tabLayout1.newTab();
        TabLayout.Tab tab3 = tabLayout1.newTab();
        TabLayout.Tab tab4 = tabLayout1.newTab();
        TabLayout.Tab tab5 = tabLayout1.newTab();
        TabLayout.Tab tab6 = tabLayout1.newTab();
        TabLayout.Tab tab7 = tabLayout1.newTab();
        TabLayout.Tab tab8 = tabLayout1.newTab();
        TabLayout.Tab tab9 = tabLayout1.newTab();
        tab1.setText("美颜0.2倍");
        tab2.setText("美颜0.4倍");
        tab3.setText("美颜0.6倍");
        tab4.setText("美颜0.8倍");
        tab5.setText("美颜1.0倍");
        tab6.setText("滤镜1");
        tab7.setText("滤镜2");
        tab8.setText("滤镜3");
        tab9.setText("滤镜4");
        tab1.setIcon(R.mipmap.ic_mofa);
        tab2.setIcon(R.mipmap.ic_mofa);
        tab3.setIcon(R.mipmap.ic_mofa);
        tab4.setIcon(R.mipmap.ic_mofa);
        tab5.setIcon(R.mipmap.ic_mofa);
        tab6.setIcon(R.mipmap.ic_mofa);
        tab7.setIcon(R.mipmap.ic_mofa);
        tab8.setIcon(R.mipmap.ic_mofa);
        tab9.setIcon(R.mipmap.ic_mofa);
        tabLayout1.addTab(tab1);
        tabLayout1.addTab(tab2);
        tabLayout1.addTab(tab3);
        tabLayout1.addTab(tab4);
        tabLayout1.addTab(tab5);
        tabLayout1.addTab(tab6);
        tabLayout1.addTab(tab7);
        tabLayout1.addTab(tab8);
        tabLayout1.addTab(tab9);
        tabLayout1.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getText().equals("美颜0.2倍")) {
                    cameraView.setBeautyLevel(0.2f);
                } else if (tab.getText().equals("美颜0.4倍")) {
                    cameraView.setBeautyLevel(0.4f);
                } else if (tab.getText().equals("美颜0.6倍")) {
                    cameraView.setBeautyLevel(0.6f);
                } else if (tab.getText().equals("美颜0.8倍")) {
                    cameraView.setBeautyLevel(0.8f);
                } else if (tab.getText().equals("美颜1.0倍")) {
                    cameraView.setBeautyLevel(1.0f);
                } else if (tab.getText().equals("滤镜1")) {
                    cameraView.updateFilter(filters.get(0));
                } else if (tab.getText().equals("滤镜2")) {
                    cameraView.updateFilter(filters.get(1));
                } else if (tab.getText().equals("滤镜3")) {
                    cameraView.updateFilter(filters.get(2));
                } else if (tab.getText().equals("滤镜4")) {
                    cameraView.updateFilter(filters.get(3));
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
//        if (!cameraView.isCameraOpened()) {
//            cameraView.openCamera(new CameraOpenListener() {
//                @Override
//                public void onCameraOpened(int i) {
//                    Log.d(TAG, "onCameraOpened: " + i);
//                }
//
//                @Override
//                public void onCameraOpenError(@NonNull Throwable throwable) {
//                    Log.d(TAG, "onCameraOpenError: " + throwable);
//                }
//            });
//        }
    }


    @OnClick({R.id.iv_switch, R.id.iv_shot, R.id.iv_type_switch, R.id.iv_close, R.id.iv_texiao_close, R.id.tv_tx_1, R.id.tv_tx_2, R.id.tv_tx_3, R.id.tv_tx_4, R.id.tv_tx_5, R.id.tv_tx_6, R.id.tv_tx_7, R.id.tv_tx_8})
    public void onClickView(View view) {
        switch (view.getId()) {
            case R.id.iv_switch:
                cameraView.switchCamera();
                break;
            case R.id.iv_shot:
                cameraView.takePicture(bitmap -> {
                    try {
                        Bitmap bitmapNew = ImageUtils.rotate(bitmap, 90, 0f, 0f);

                        ivImg.setImageBitmap(bitmapNew);
                        clPic.setVisibility(View.VISIBLE);
                    } catch (Exception e) {
                        LogUtils.dTag(TAG, e);
                    }
                });

                break;

            case R.id.iv_type_switch:
                if (tabLayout1.getVisibility() == View.VISIBLE) {
                    tabLayout1.setVisibility(View.GONE);
                    cameraView.enableBeauty(false);
                } else {
                    tabLayout1.setVisibility(View.VISIBLE);
                    cameraView.enableBeauty(true);
                }
//                if (cameraView.getMediaType() == MediaType.TYPE_PICTURE) {
//                    ivTypeSwitch.setImageResource(R.drawable.ic_videocam_white_24dp);
//                    cameraView.setMediaType(MediaType.TYPE_VIDEO);
//                } else {
//                    ivTypeSwitch.setImageResource(R.drawable.ic_photo_camera_white_24dp);
//                    cameraView.setMediaType(MediaType.TYPE_PICTURE);
//                }
                break;
            case R.id.iv_close:
                clPic.setVisibility(View.GONE);
                cameraView.reStartCameraView();
                ivImg.setImageBitmap(null);
                break;
            case R.id.iv_texiao_close:
                tabLayout1.setVisibility(View.GONE);
                break;
            case R.id.tv_tx_1:
                ToastUtils.showShort("选择了特效1");
                clMenu.setVisibility(View.GONE);
                break;
            case R.id.tv_tx_2:
                ToastUtils.showShort("选择了特效2");
                clMenu.setVisibility(View.GONE);
                break;
            case R.id.tv_tx_3:
                ToastUtils.showShort("选择了特效3");
                clMenu.setVisibility(View.GONE);
                break;
            case R.id.tv_tx_4:
                ToastUtils.showShort("选择了特效4");
                clMenu.setVisibility(View.GONE);
                break;
            case R.id.tv_tx_5:
                ToastUtils.showShort("选择了特效5");
                clMenu.setVisibility(View.GONE);
                break;
            case R.id.tv_tx_6:
                ToastUtils.showShort("选择了特效6");
                clMenu.setVisibility(View.GONE);
                break;
            case R.id.tv_tx_7:
                ToastUtils.showShort("选择了特效7");
                clMenu.setVisibility(View.GONE);
                break;
            case R.id.tv_tx_8:
                ToastUtils.showShort("选择了特效8");
                clMenu.setVisibility(View.GONE);
                break;

        }
    }

}
